﻿using System;
using Microsoft.SharePoint.WebControls;
using TSB.FarmPowerShellWizard.ControlTemplates.TSB.FarmPowerShellWizard;
using TSB.FarmPowerShellWizard.Repository;

namespace TSB.FarmPowerShellWizard.Layouts.TSB.FarmPowerShellWizard
{
    public partial class PSW_Farm : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnServers.Click += btnServers_Click;
            btnManagedAccounts.Click += new EventHandler(btnManagedAccounts_Click);
        }

        void btnManagedAccounts_Click(object sender, EventArgs e)
        {
            var farmRepository = new FarmRepository();

            SetPowerShellText(farmRepository.GetManagedAccountsScript());
        }

        void btnServers_Click(object sender, EventArgs e)
        {
            var farmRepository = new FarmRepository();

            SetPowerShellText(farmRepository.GetServerScript());
        }

        void SetPowerShellText(string script)
        {
            if (string.IsNullOrEmpty(script))
            {
                phScriptContainer.Visible = false;
                txtPowerShell.Text = string.Empty;
                return;
            }

            phScriptContainer.Visible = true;
            txtPowerShell.Text = script;
        }
    }
}
